//
//  ZYReadTableViewCell.m
//  MusicMV
//
//  Created by Eternity on 16/3/17.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "ZYReadTableViewCell.h"

@interface ZYReadTableViewCell ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, assign) CGFloat picheight;
@property (nonatomic, retain) UICollectionView *collectionView;//#。。。
@property (nonatomic, retain) UICollectionView *pCollectionView;// 评论
@property (nonatomic, assign) BOOL isSelect;
@end

@implementation ZYReadTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _isSelect = YES;
        
        self.touImage = [[UIImageView alloc] init];
        _touImage.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_touImage];
        [_touImage mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(5);
            make.top.mas_equalTo(5);
            make.size.mas_equalTo(CGSizeMake(40, 40));
            
            
        }];
        
        self.touLabel = [[UILabel alloc] init];
        _touLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_touLabel];
        
        [_touLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(_touImage.mas_right).offset(5);
            make.top.mas_equalTo(_touImage.mas_top);
            make.size.mas_equalTo(CGSizeMake(150, 40));
            
        }];
        
        
        self.sTouLabel = [[UILabel alloc] init];
        _sTouLabel.backgroundColor = [UIColor clearColor];
        _sTouLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_sTouLabel];
        
        [_sTouLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.right.mas_equalTo(-10);
            make.top.mas_equalTo(_touImage.mas_top);
            make.size.mas_equalTo(CGSizeMake(70, 40));
            
        }];
    
        self.bigImage = [[UIImageView alloc] init];
        _bigImage.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_bigImage];
        
        [self createViews];
        
    }
    
    return self;
}
- (void)dealloc {
    
}

- (void)setModel:(ZYReadModel *)model {
    
    _model = model;
    
    
    CGFloat picweight = SCREEN_WIDTH;
    _picheight = [_model.height floatValue] / [_model.width floatValue] * picweight;
    
//    _label.frame = CGRectMake(0, _picheight * ht + 100, 375 * wt, 40 * ht);
//    _bigImage.frame = CGRectMake(0, 50, picweight, _picheight );
    
    [_bigImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(_touImage.mas_bottom);
        make.height.mas_equalTo(_picheight);
    }];
    
    _label.text = _model.content;
    [_bigImage sd_setImageWithURL:[NSURL URLWithString:_model.photo]];
    [_touImage sd_setImageWithURL:[NSURL URLWithString:_model.avatar]];
    _touLabel.text = _model.uname;
    _sTouLabel.text = [NSString stringWithFormat:@"🕒%@", _model.format_time];
    
    [_collectionView reloadData];
    
}

- (void)createViews {
    
    
//    if (![_model.content isEqualToString:@""]) {
    
        self.label = [[UILabel alloc] init];
        _label.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_label];
//    }

    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    [self.contentView addSubview:_collectionView];
    
    UICollectionViewFlowLayout *pLayout = [[UICollectionViewFlowLayout alloc] init];
//    pLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 40) / 3, 50 * ht);
    pLayout.minimumInteritemSpacing = 10;
    pLayout.minimumLineSpacing = 10;
    pLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.pCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:pLayout];
    
    [self.contentView addSubview:_pCollectionView];
    
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
//    _label.frame = CGRectMake(0, _picheight * ht + 100, 375 * wt, 40 * ht);
    
    
//    _collectionView.frame = CGRectMake(0, _picheight + 50, SCREEN_WIDTH, 50 * ht);
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(_bigImage.mas_bottom);
        make.height.mas_equalTo(50);
        
    }];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[LZYCollectionViewCell class] forCellWithReuseIdentifier:@"LZYCollectionViewCell"];
    
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(_collectionView.mas_bottom);
        make.height.mas_equalTo(40);
        
    }];

//    _pCollectionView.frame = CGRectMake(0, _picheight + 140, SCREEN_WIDTH, 50 * ht);
    
    [_pCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(_label.mas_bottom);
        make.height.mas_equalTo(50);
        
    }];
    
    _pCollectionView.delegate = self;
    _pCollectionView.dataSource = self;
    _pCollectionView.backgroundColor = [UIColor whiteColor];
    [_pCollectionView registerClass:[ZYPCollectionViewCell class] forCellWithReuseIdentifier:@"ZYPCollectionViewCell"];
    
}

#pragma mark - collecitonView item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == _collectionView) {
        
        return _model.tags.count;
    } else {
        return 3;
        
    }
    
}
#pragma mark - collectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _collectionView) {
        
        if (_model.tags.count != 0) {
            LZYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LZYCollectionViewCell" forIndexPath:indexPath];
            ZYModel *model = _model.tags[indexPath.row];
            cell.label.text = model.name;
            
            return cell;
        }
    } else {
        
        ZYPCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYPCollectionViewCell" forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            
            cell1.label.text = [NSString stringWithFormat:@"%@", _model.liked_num];
            cell1.imgeView.image = [UIImage imageNamed:@"iconfont-zan.png"];
            return cell1;
        } else if (indexPath.row == 1) {
            
            cell1.label.text = [NSString stringWithFormat:@"%@", _model.commented_num];
            cell1.imgeView.image = [UIImage imageNamed:@"iconfont-sangedian.png"];
            return cell1;
        } else if (indexPath.row == 2) {
            
            cell1.label.text = @"更多";
            cell1.imgeView.image = [UIImage imageNamed:@"iconfont-jinlingyingcaiwangtubiao41-2.png"];
            return cell1;
        }
        
        
        
        
    }
    return nil;
}
#pragma mark - item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _collectionView) {
        
        return CGSizeMake(50, 30);
    }
    return CGSizeMake((SCREEN_WIDTH - 40) / 3, 50 * ht);;
    
}
#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == _pCollectionView) {
        if (indexPath.row == 0) {
            // 获取cell
            ZYPCollectionViewCell *cell = (ZYPCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
            
//            ZYPCollectionViewCell *cell = [[ZYPCollectionViewCell alloc] init];
            // 关键帧动画
            CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            // 设置关键帧的值
            keyAnimation.values = @[@(1.0), @(0.1), @(1.0), @(1.1), @(1.0)];
            // 设置动画执行的时间
            keyAnimation.duration = 1;
            // 添加核心动画
            [cell.imgeView.layer addAnimation:keyAnimation forKey:nil];
            
            if (_isSelect == YES) {
                [cell.imgeView setImage:[UIImage imageNamed:@"IconHgood@2x.png"]];
            }else {
                
                [cell.imgeView setImage:[UIImage imageNamed:@"iconfont-zan.png"]];
                
            }
            _isSelect = !_isSelect;
            
            
        } else if (indexPath.row == 1) {
            
            [self.delegate ZYPCollectionViewCellDelegate:_model];
            
            
        }
    }
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
